/*    */ package com.cr.api.controller;
import com.cr.api.model.LawFirmAuthenticationModel;
import com.cr.api.model.LawyerAuthenticationModel;
/*    */
/*    */ import com.cr.api.model.LoginModel;
/*    */ import com.cr.api.model.RegisterModel;
/*    */ import com.cr.api.model.UpdateUserModel;
import com.cr.api.model.UserAuthenticationModel;
import com.cr.api.vo.UserVo;
/*    */ import com.cr.business.domain.CrUser;
import com.cr.business.dto.RankingListDto;
/*    */ import com.cr.business.service.ICrUserService;
/*    */ import com.cr.common.constant.Constants;
import com.cr.common.core.controller.BaseController;
/*    */ import com.cr.common.core.domain.AjaxResult;
import com.cr.common.utils.CacheUtils;
import com.github.pagehelper.PageInfo;

/*    */ import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/*    */ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
/*    */ import org.springframework.beans.BeanUtils;
/*    */ import org.springframework.beans.factory.annotation.Autowired;
/*    */ import org.springframework.dao.DuplicateKeyException;
import org.springframework.util.DigestUtils;
/*    */ import org.springframework.web.bind.annotation.*;
/*    */
/*    */
/*    */

/*    */ @Api(description = "用户管理")
/*    */ @RestController
/*    */ @RequestMapping({"/api/user"})
@CrossOrigin
/*    */ public class ApiUserController extends BaseController {
    /*    */   @Autowired
    /*    */   private ICrUserService crUserService;
    /*    */
    /*     */  @ApiOperation("用户登录")
    /*     */  @ApiImplicitParams({@ApiImplicitParam(name = "loginId", value = "登录账号", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "用户密码", dataType = "String")})
    /*    */   @PostMapping({"/login"})
    /*    */   @ResponseBody
    /*    */   public AjaxResult login(@RequestBody LoginModel model) {
        /* 38 */     if (StringUtils.isEmpty(model.getLoginId()))
            /* 39 */       return AjaxResult.error("登录账号不能空");
        /* 41 */     if (StringUtils.isEmpty(model.getPassword()))
            /* 42 */       return AjaxResult.error("密码不能空");
        if (StringUtils.isEmpty(model.getUuid()))
            return AjaxResult.error("uuid不能空");
        if (StringUtils.isEmpty(model.getCode()))
            return AjaxResult.error("验证码不能空");
        String verifyKey = Constants.CAPTCHA_CODE_KEY + model.getUuid();
        if(!model.getCode().equalsIgnoreCase(CacheUtils.get(verifyKey).toString()))
            return AjaxResult.error("验证码不正确");
        /* 45 */     CrUser user = this.crUserService.selectCrUserByloginId(model.getLoginId());
        /* 46 */     if (user == null)
            /* 47 */       return AjaxResult.error("用户不存在");
        /* 49 */     //String pwd = DigestUtils.md5DigestAsHex(model.getPassword().getBytes());
        /* 49 */     String pwd = model.getPassword();
        /* 50 */     if (!pwd.equals(user.getPassword()))
            /* 51 */       return AjaxResult.error("密码错误");
        /* 53 */     return AjaxResult.success(user);
        /*    */   }
    /*    */
    /*    */   @PostMapping({"/register"})
    /*    */   @ResponseBody
    /*     */  @ApiOperation("用户注册")
    /*     */  public AjaxResult register(@RequestBody RegisterModel model) {
//                if (StringUtils.isEmpty(model.getUuid()))
//                    return AjaxResult.error("uuid不能空");
                if (StringUtils.isEmpty(model.getCode()))
                    return AjaxResult.error("验证码不能空");
                Object code = CacheUtils.get("smscode"+model.getMobile());
                if(!model.getCode().equals(code.toString())){
                    return AjaxResult.error("验证码错误");
                }
    /* 64 */     CrUser user = new CrUser();
    /* 65 */     BeanUtils.copyProperties(model, user);
    /* 66 */     user.setLoginId(model.getMobile());
    /* 67 */     user.setCreateTime(new Date());
    /* 68 */
                try {
                    Integer i = Integer.valueOf(this.crUserService.insertCrUser(user));
                    return toAjax(i.intValue());
                } catch (DuplicateKeyException e) {
                    return AjaxResult.error("手机号码已存在,请核实");
                }
    /*    */  }
    /*    */
    /**
     * 修改信息
     * @param model
     * @return
     */
    @ApiOperation("修改信息")
    @PostMapping("/updateUser")
    @ResponseBody
    public AjaxResult updateUser(@RequestBody UpdateUserModel model){
        if(model.getUserId() == null){
            return AjaxResult.error("用户id不能为空");
        }
        CrUser crUser = crUserService.selectCrUserByUserId(model.getUserId());
        if(crUser == null){
            return AjaxResult.error("用户不存在");
        }
        BeanUtils.copyProperties(model, crUser);
        return toAjax(crUserService.updateCrUser(crUser));
    }

    /**
     * 律所认证
     * @param model
     * @return
     */
    @ApiOperation("律所认证")
    @PostMapping("/LawFirmAuthentication")
    @ResponseBody
    public AjaxResult LawFirmAuthentication(HttpServletRequest request,@RequestBody LawFirmAuthenticationModel model){
        if(model.getUserId() == null){
            return AjaxResult.error("用户id不能为空");
        }
        CrUser crUser = crUserService.selectCrUserByUserId(model.getUserId());
        if(crUser == null){
            return AjaxResult.error("用户不存在");
        }
        BeanUtils.copyProperties(model, crUser);
        crUser.setAuditStatus(0);
        return toAjax(crUserService.updateCrUser(crUser));
    }

    /**
     * 律师认证
     * @param model
     * @return
     */
    @ApiOperation("律师认证")
    @PostMapping("/LawyerAuthentication")
    @ResponseBody
    public AjaxResult LawyerAuthentication(HttpServletRequest request,@RequestBody LawyerAuthenticationModel model){
        if(model.getUserId() == null){
            return AjaxResult.error("用户id不能为空");
        }
        Object code = CacheUtils.get("smscode"+model.getMobile());
        if(!model.getCode().equals(code.toString())){
            return AjaxResult.error("验证码错误");
        }
        CrUser crUser = crUserService.selectCrUserByUserId(model.getUserId());
        if(crUser == null){
            return AjaxResult.error("用户不存在");
        }
        BeanUtils.copyProperties(model, crUser);
        crUser.setAuditStatus(0);
        return toAjax(crUserService.updateCrUser(crUser));
    }

    /**
     *排行榜
*/
    @ApiOperation("排行榜")
    @PostMapping("/getServiceList")
    @ApiImplicitParam(name = "userType", value = "用户类型 0：个人 1：律师", required = true, dataType = "Integer")
    @ResponseBody
    public AjaxResult getServiceList(@RequestBody CrUser param)
    {
        Integer userType =param.getUserType();
        startPage();
        List<RankingListDto> rankingListDtos = crUserService.selectRankingList(0);
        return AjaxResult.success(new PageInfo<>(rankingListDtos));
    }

/**
  * 排行榜
*/
    @ApiOperation("排行榜金额详情")
    @PostMapping("/getServiceListAmountDetail")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Long")
    @ResponseBody
    public AjaxResult getServiceListAmount(@RequestBody CrUser param)
    {
    Long userId = param.getUserId();
    startPage();
    List<RankingListDto> rankingListDtos = crUserService.selectRankingDetail(userId);
    return AjaxResult.success(new PageInfo<>(rankingListDtos));
    }
    
    /**
     * 用户信息
     */
    @ApiOperation("用户信息")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "Long")
    @PostMapping("/getUserInfo")
    @ResponseBody
    public AjaxResult getUserInfo(@RequestBody CrUser param)
    {
        Long userId = param.getUserId();
        CrUser user = crUserService.selectCrUserByUserId(userId);
        UserVo vo = new UserVo();
        BeanUtils.copyProperties(user, vo);
        return AjaxResult.success(vo);
    }

    /**
     * 个人认证
     * @param model
     * @return
     */
    @ApiOperation("个人认证")
    @PostMapping("/userAuthentication")
    @ResponseBody
    public AjaxResult userAuthentication(HttpServletRequest request,@RequestBody UserAuthenticationModel model){
        if(model.getUserId() == null){
            return AjaxResult.error("用户id不能为空");
        }
        Object code = CacheUtils.get("smscode"+model.getMobile());

//        if(!model.getCode().equals(code.toString())){
//            return AjaxResult.error("验证码错误");
//        }
        CrUser crUser = crUserService.selectCrUserByUserId(model.getUserId());
        if(crUser == null){
            return AjaxResult.error("用户不存在");
        }
        BeanUtils.copyProperties(model, crUser);
        crUser.setAuditStatus(0);
        return toAjax(crUserService.updateCrUser(crUser));
    }

    /*    */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-api-4.7.0.jar!\com\cr\api\controller\ApiUserController.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */